//
//  HELChooseCategoryVC.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/11.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "HELChooseCategoryVC.h"
#import "HELNormalCell.h"
#import "HELCatogryModel.h"
@interface HELChooseCategoryVC ()

@end

@implementation HELChooseCategoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self requestCategoryList];
    
    
}

#pragma -mark 请求课程类别列表

- (void)requestCategoryList{
  
    weakSelf_define;
    weakSelf.tableView.loading = YES;
    [[HELNetworkManager sharedMg] GET:kCourseCatogry parameters:nil success:^(NSDictionary * responseObject) {
        [weakSelf.tableView.header endRefreshing];
        weakSelf.tableView.loading  =NO;
        [weakSelf.dataArray removeAllObjects];
       
        NSArray *array = [HELCatogryModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        [weakSelf.dataArray addObjectsFromArray:array];
        [weakSelf.tableView reloadData];
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        weakSelf.tableView.loading  =NO;
    }];

    
    
}

- (void)configUI{

    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight -49);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELNormalCell" bundle:nil] forCellReuseIdentifier:@"HELNormalCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"课程分类选择";
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf requestCategoryList];
    }];
    [weakSelf.tableView.header beginRefreshing];
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return  self.dataArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELNormalCell *cell =[tableView dequeueReusableCellWithIdentifier:@"HELNormalCell"];
    
    UIView *view = [UIView new];
    view.alpha = .7;
    view.frame = cell.bounds;
    view.backgroundColor = kMainColor;
    cell.selectedBackgroundView =view;
//     *data =self.dataArray[indexPath.section][indexPath.row];
//    cell.mainTitleL.text = data[@"name"];
    if (indexPath.section == 0) {
        cell.mainTitleL.text  =@"全部";
    }else{
        HELCatogryModel *model = self.dataArray[indexPath.row];
        cell.mainTitleL.text = model.name;
        
    }
    
    
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        self.callBack(@"全部",nil);
    }else{
     HELCatogryModel * cate = self.dataArray[indexPath.row];
        self.callBack(cate.name,@([cate.id integerValue]));
    }
   
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view =[UIView new];
    view.frame =CGRectMake(0, 0, kScreenWidth, 10);
    view.backgroundColor =[UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 12;
}



@end
